package com.hgx.su.common.pure.dto;

import com.hgx.su.common.pure.constants.GeneralConstant;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 结果返回体
 */
public class ResultResponse<T> implements Serializable {
    /**
     * 公共返回参数
     */
    private PubResponse pubResponse;
    /**
     * 返回内容
     */
    private T body;

    public ResultResponse() {
    }

    public ResultResponse(T body) {
        this.body = body;
    }

    public ResultResponse(PubResponse pubResponse, T body) {
        this.pubResponse = pubResponse;
        this.body = body;
    }


    public static <T> ResultResponse<ListPackageDto<T>> ok(List<T> list) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, list);
    }

    public static <T> ResultResponse<ListPackageDto<T>> ok(PubResponse pubResponse, List<T> list) {
        ListPackageDto<T> packageDto = new ListPackageDto<T>();
        packageDto.setList(list);

        return new ResultResponse<>(pubResponse, packageDto);
    }


    public static ResultResponse<NullClassDto> ok() {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse);
    }

    public static ResultResponse<NullClassDto> ok(PubResponse pubResponse) {
        return new ResultResponse<>(pubResponse, new NullClassDto());
    }


    public static <T> ResultResponse<T> ok(T data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }

    public static <T> ResultResponse<T> ok(PubResponse pubResponse, T data) {
        return new ResultResponse<>(pubResponse, data);
    }


    public static ResultResponse<OnePackageDto<Byte>> ok(Byte data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);
        return ok(pubResponse, data);
    }

    public static ResultResponse<OnePackageDto<Byte>> ok(PubResponse pubResponse, Byte data) {
        OnePackageDto<Byte> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }


    public static ResultResponse<OnePackageDto<Short>> ok(Short data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }

    public static ResultResponse<OnePackageDto<Short>> ok(PubResponse pubResponse, Short data) {

        OnePackageDto<Short> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }


    public static ResultResponse<OnePackageDto<Integer>> ok(Integer data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }


    public static ResultResponse<OnePackageDto<Integer>> ok(PubResponse pubResponse, Integer data) {

        OnePackageDto<Integer> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }


    public static ResultResponse<OnePackageDto<Long>> ok(Long data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);
        return ok(pubResponse, data);
    }

    public static ResultResponse<OnePackageDto<Long>> ok(PubResponse pubResponse, Long data) {

        OnePackageDto<Long> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }


    public static ResultResponse<OnePackageDto<String>> ok(String data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }

    public static ResultResponse<OnePackageDto<Float>> ok(Float data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }

    public static ResultResponse<OnePackageDto<Double>> ok(Double data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }

    public static ResultResponse<OnePackageDto<Character>> ok(Character data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }


    public static ResultResponse<OnePackageDto<BigDecimal>> ok(BigDecimal data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }

    public static ResultResponse<OnePackageDto<Boolean>> ok(Boolean data) {

        PubResponse pubResponse = createDefaultPubResponse(GeneralConstant.SUCCESS_CODE, GeneralConstant.SUCCESS_DESC);

        return ok(pubResponse, data);
    }


    public static ResultResponse<OnePackageDto<String>> ok(PubResponse pubResponse, String data) {

        OnePackageDto<String> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }

    public static ResultResponse<OnePackageDto<Float>> ok(PubResponse pubResponse, Float data) {

        OnePackageDto<Float> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }

    public static ResultResponse<OnePackageDto<Double>> ok(PubResponse pubResponse, Double data) {

        OnePackageDto<Double> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }

    public static ResultResponse<OnePackageDto<Character>> ok(PubResponse pubResponse, Character data) {

        OnePackageDto<Character> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }


    public static ResultResponse<OnePackageDto<BigDecimal>> ok(PubResponse pubResponse, BigDecimal data) {

        OnePackageDto<BigDecimal> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }

    public static ResultResponse<OnePackageDto<Boolean>> ok(PubResponse pubResponse, Boolean data) {

        OnePackageDto<Boolean> onePackageDto = new OnePackageDto<>();

        onePackageDto.setData(data);

        return new ResultResponse<>(pubResponse, onePackageDto);
    }


    public static ResultResponse<NullClassDto> error(String s, String description, String module, String statusCode) {

        PubResponse defaultPubResponse = createDefaultPubResponse(s, description, module, statusCode);
        return error(defaultPubResponse);
    }

    /**
     * 错误
     *
     * @param code        错误码
     * @param description 描述
     * @return 错误返回bean
     */
    public static ResultResponse<NullClassDto> error(String code, String description) {
        PubResponse pubResponse = createDefaultPubResponse(code, description);

        return error(pubResponse);
    }


    /**
     * 错误
     *
     * @return 错误返回bean
     */
    public static ResultResponse<NullClassDto> error(PubResponse pubResponse) {

        NullClassDto nullClassDto = new NullClassDto();

        return new ResultResponse<>(pubResponse, nullClassDto);
    }

    /**
     * 创建默认的公共返回参数
     *
     * @param code        错误码
     * @param description 描述
     * @return 公共返回参数
     */
    public static PubResponse createDefaultPubResponse(String code, String description) {

        String module = "0000";
        String statusCode = code;
        if (!code.equals("200")) {
            module = code.substring(0, 4);
            statusCode = code.substring(4);
        }
        return createDefaultPubResponse(code, description, module, statusCode);
    }


    /**
     * 创建默认的公共返回参数
     *
     * @param code        错误码
     * @param description 描述
     * @return 公共返回参数
     */
    public static PubResponse createDefaultPubResponse(String code, String description, String module, String statusCode) {
        PubResponse pubResponse = new PubResponse();
        pubResponse.setCode(code);
        pubResponse.setModule(module);
        pubResponse.setStatusCode(statusCode);
        pubResponse.setMsg(description);
        pubResponse.setToken("");
        pubResponse.setSign("");
        pubResponse.setSignType("");
        pubResponse.setVersion("1.0");
        return pubResponse;
    }


    public PubResponse getPubResponse() {
        return pubResponse;
    }

    public void setPubResponse(PubResponse pubResponse) {
        this.pubResponse = pubResponse;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "ResultResponse{" +
                "pubResponse=" + pubResponse +
                ", body=" + body +
                '}';
    }

}
